package net.lab1024.sa.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;

import java.time.Duration;

@Configuration
public class WebClientConfig {

    @Bean
    public WebClient webClient() {
        // 1. 配置底层 HTTP 客户端（如设置超时）
        HttpClient httpClient = HttpClient.create()
                .responseTimeout(Duration.ofSeconds(5)); // 响应超时 5 秒

        // 2. 通过 WebClient.Builder 构建 WebClient
        return WebClient.builder()
                .clientConnector(new ReactorClientHttpConnector(httpClient)) // 绑定自定义 HttpClient
                .defaultHeader("Content-Type", "application/json") // 添加默认请求头
//                .baseUrl("https://api.example.com") // 设置基础 URL（可选）
                .build();
    }

}